package com.yidu.labyrinth.service;

import com.yidu.labyrinth.dao.LabyrinthModuleDao;
import com.yidu.labyrinth.main.LabyrinthModuleEntity;
import com.yidu.labyrinth.main.LabyrinthUserEntity;
import com.yidu.labyrinth.vo.LabyrinthVo;
import com.yidu.labyrinth.vo.Pages;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * @author 朱星润
 * @类的描述——
 * @date 2019/09/26上午 11:09
 */
public class LabyrinthModuleServiceImpl implements LabyrinthModuleService {
    private LabyrinthModuleDao dao;

    public LabyrinthModuleDao getDao() {
        return dao;
    }

    public void setDao(LabyrinthModuleDao dao) {
        this.dao = dao;
    }

    @Override
    public List<LabyrinthVo> findAll(LabyrinthVo vo, Pages pages) {
        //模块模型
        LabyrinthModuleEntity params = new LabyrinthModuleEntity();
        //将vo值赋给po
        BeanUtils.copyProperties(vo, params);
        List<LabyrinthModuleEntity> listPo = dao.findAll(params, pages);
        List<LabyrinthVo> listVo = new ArrayList<LabyrinthVo>();
        for (Iterator<LabyrinthModuleEntity> iterator = listPo.iterator(); iterator.hasNext(); ) {
            LabyrinthModuleEntity modelPo = iterator.next();
            LabyrinthVo modelVo = new LabyrinthVo();
            //将po值赋给vo
            BeanUtils.copyProperties(modelPo, modelVo);
            if (modelPo.getModuleIds() != null) {
                LabyrinthModuleEntity m= dao.findById(modelPo.getModuleIds());
                if(m!=null){
                    modelVo.setModuleNames(m.getModuleName());
                }

            }
            //放入集合
            listVo.add(modelVo);

        }


        //返回集合
        return listVo;
    }

    @Override
    public LabyrinthVo findById(String id) {
        LabyrinthModuleEntity model = dao.findById(id);
        LabyrinthVo vo = new LabyrinthVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    @Override
    public Long count(LabyrinthVo vo) {
        return dao.count(vo);
    }

    /**
     * vo转换po
     *
     * @param vo
     * @return
     */
    private LabyrinthModuleEntity addOrUpdate(LabyrinthVo vo) {
        LabyrinthModuleEntity po = new LabyrinthModuleEntity();
        BeanUtils.copyProperties(vo, po);
        return po;
    }

    @Override
    public int update(LabyrinthVo vo) {

        return dao.save(addOrUpdate(vo));
    }

    @Override
    public int save(LabyrinthVo vo) {
        LabyrinthModuleEntity po = new LabyrinthModuleEntity();
        if (vo.getModuleId() != null && !vo.getModuleId().equals("")) {//修改
            LabyrinthVo lvo = findById(vo.getModuleId());
            System.out.println(lvo.getModuleSort()+lvo.getModuleIds()+lvo.getModuleRemark()+"sadasd");
            BeanUtils.copyProperties(vo, po);
            po.setModuleIds(lvo.getModuleIds());
            po.setModuleRemark(lvo.getModuleRemark());
            po.setModuleSort(lvo.getModuleSort());
            po.setModuleResult(lvo.getModuleResult());
            po.setModuleId(lvo.getModuleId());
        }else{//增加
            BeanUtils.copyProperties(vo, po);
            if(po.getModuleIds().equals("")){
                po.setModuleIds("1");
            }
            po.setModuleId(UUID.randomUUID().toString().replaceAll("-",""));
            po.setModuleSort(1L);//排序
            po.setModuleRemark("无");//备注
            po.setModuleResult(1);
        }

        return dao.save(po);
    }

    /**
     * 查询所有不分页
     */
    @Override
    public List<LabyrinthVo> findAllModule() {
        List<LabyrinthModuleEntity> list = dao.findAllModule();
        List<LabyrinthVo> listVo = new ArrayList<LabyrinthVo>();

        for (Iterator<LabyrinthModuleEntity> iterator = list.iterator(); iterator.hasNext(); ) {
            LabyrinthModuleEntity modelPo = iterator.next();
            LabyrinthVo modelVo = new LabyrinthVo();
            BeanUtils.copyProperties(modelPo, modelVo);
            listVo.add(modelVo);
        }

        return listVo;
    }

    /**
     * 框架
     */
    @Override
    public List<LabyrinthVo> X_admin() {
        List<LabyrinthModuleEntity> list = dao.X_admin();
        List<LabyrinthVo> listVo = new ArrayList<LabyrinthVo>();

        for (Iterator<LabyrinthModuleEntity> iterator = list.iterator(); iterator.hasNext(); ) {
            LabyrinthModuleEntity modelPo = iterator.next();
            LabyrinthVo modelVo = new LabyrinthVo();
            BeanUtils.copyProperties(modelPo, modelVo);
            listVo.add(modelVo);
        }

        return listVo;
    }

}
